package com.dlwx.parttimetalent.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/12/27.
 */

public class CallPhoneUtil {
    public static void call(final Context context, final String phone_num){
        RxPermissions rxpermission=new RxPermissions((Activity) context);
        if(TextUtils.isEmpty(phone_num)){
            ToastUtils.toastForShort(context,"电话号码为空");
            return;
        }
        rxpermission.request(android.Manifest.permission.CALL_PHONE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone_num));
                            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                return;
                            }
                            context.startActivity(intent);
                        }
                     }
                });
    }
}